import pymongo

def update_information(form , ex_info_dateset , uid ):
    client = pymongo.MongoClient("mongodb://localhost:27017/")
    db = client["ACM_MEMBER"]
    collection = db[form]

    base_dateset = db[ex_info_dateset]
    base_info = []
    for team_information in collection.find():
        data = []
        data.append(team_information["student_id"])
        data.append(team_information["name"])
        data.append(team_information[uid])
        if form == "TEAM_INFORMATION":
            data.append(True)
        else:
            data.append(False)
        base_info.append(data)
    for info in base_info:
        ty = base_dateset.find_one({"student_id": info[0]})
        if not base_dateset.find_one({"student_id": info[0]}):
            base_dateset.insert_one(
                {"student_id": info[0], "name": info[1], uid: info[2], "is_team": info[3]})
            print(str(info[0]) + "插入成功")
        elif ty[uid] != info[2] or ty["is_team"] != info[3]:
            base_dateset.update_one({"student_id": info[0]},
                                        {"$set": {uid: info[2], "is_team": info[3]}})
            print(str(info[0]) + "更新成功")
        else:
            pass

    client.close()
def update_team_information( ex_info_dateset , uid):
    update_information("TEAM_INFORMATION", ex_info_dateset , uid)

def update_student_information( ex_info_dateset , uid):
    update_information("OUT_TEAM_INFORMATION", ex_info_dateset , uid)

def update_all_information( ex_info_dateset , uid):
    update_team_information( ex_info_dateset , uid)
    update_student_information( ex_info_dateset , uid)

if __name__ == "__main__":
    ex_info_dateset = "BASE_INFORMATION"
    uid = "luogu_id"
    update_team_information( ex_info_dateset , uid)
    update_student_information( ex_info_dateset , uid)